﻿using System;
using System.Collections.Generic;
using System.Text;
using Telerik.Web.UI;
using Acms.Bll;
using System.Web.UI.WebControls;
using System.Web.UI;
using Acms.AcmsControls.ContentControls.FrontEnd;

namespace Acms.AcmsControls.ContentControls.Admin
{
    public class ImageAdminEdit : BaseAdminEdit<ImageContentControlProperties>
    {
        protected override string GetCssClass
        {
            get { return "imageContainer"; }
        }

        protected override void SaveToProperties(ImageContentControlProperties properties, PostControl control)
        {
            if (upload.UploadedFiles.Count == 1)
            {
                File file = File.CreateInstance(File.FileTypes.Image, upload.UploadedFiles[0].GetNameWithoutExtension(), upload.UploadedFiles[0].ContentType, upload.UploadedFiles[0].ContentLength, upload.UploadedFiles[0].InputStream);
                
                file.BeginTransaction();
                control.UseTransaction(file.TransactionKey);
                
                file.Update();

                properties.ResourceId = file.Id;
                properties.Height = file.Height.Value;
                properties.Width = file.Width.Value;
            }
            properties.AltText = altText.Text;
        }

        public override void PropertiesLoaded(ImageContentControlProperties properties)
        {
            altText.Text = properties.AltText;
            image.FileId = properties.ResourceId;
            imageDimentions.Text = string.Format("{0}px x {1}px", properties.Width, properties.Height);
        }

        private RadUpload upload = new RadUpload();
        private TextBox altText = new TextBox();
        private AcmsImage image = new AcmsImage();
        private Label imageDimentions = new Label();

        protected override void CreateChildControls()
        {
            upload.ControlObjectsVisibility = ControlObjectsVisibility.None;
            this.Controls.Add(upload);

            this.Controls.Add(altText);

            image.RenderHeight = 400;
            image.RenderWidth = 600;
            this.Controls.Add(image);

            this.Controls.Add(imageDimentions);
            base.CreateChildControls();
        }

        protected override void RenderContents(System.Web.UI.HtmlTextWriter writer)
        {
            upload.RenderControl(writer);
            writer.AddAttribute(HtmlTextWriterAttribute.Class, "form");
            writer.RenderBeginTag(HtmlTextWriterTag.Ul);
            writer.RenderBeginTag(HtmlTextWriterTag.Li);

            writer.AddAttribute(HtmlTextWriterAttribute.For, altText.ClientID);
            writer.RenderBeginTag(HtmlTextWriterTag.Label);
            writer.Write("Alternate Text:");
            writer.RenderEndTag();//lable

            writer.RenderBeginTag(HtmlTextWriterTag.Span);
            altText.RenderControl(writer);
            writer.RenderEndTag();//span

            writer.RenderEndTag();//li
            writer.RenderEndTag();//ul

            if (image.FileId > 0)
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Class, "form");
                writer.RenderBeginTag(HtmlTextWriterTag.Ul);

                writer.RenderBeginTag(HtmlTextWriterTag.Li);
                writer.AddAttribute(HtmlTextWriterAttribute.For, image.ClientID);
                writer.RenderBeginTag(HtmlTextWriterTag.Label);
                writer.Write("Existing Image:");
                writer.RenderEndTag();//lable

                writer.RenderBeginTag(HtmlTextWriterTag.Span);
                image.RenderControl(writer);
                writer.RenderEndTag();//span

                writer.RenderEndTag();//li

                writer.RenderBeginTag(HtmlTextWriterTag.Li);

                writer.AddAttribute(HtmlTextWriterAttribute.For, imageDimentions.ClientID);
                writer.RenderBeginTag(HtmlTextWriterTag.Label);
                writer.Write("Dimensions:");
                writer.RenderEndTag();//lable

                imageDimentions.RenderControl(writer);

                writer.RenderEndTag();//li

                writer.RenderEndTag();//ul
            }
        }
    }
}
